package com.qdaily.ui.data.DataBase;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ReadDAO {

    private DBHelper dbOpenHelper;
    private static ReadDAO readDAO;

    private ReadDAO() {
        this.dbOpenHelper = DBHelper.dbHelper();
    }

    public static ReadDAO getInstance() {
        if (readDAO == null) {
            readDAO = new ReadDAO();
        }
        return readDAO;
    }

    public void read(int server_id, String type) {
        if (!isRead(server_id, type)) {
            SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("server_id", server_id + "");
            values.put("type", type);
            db.insert("readHistory2", null, values);
        }
    }

    public boolean isRead(int server_id, String type) {
        if (type == null)
            return false;
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from readHistory2 where server_id=? and type=? limit 1", new String[]{server_id + "", type});
        boolean result = false;
        if (cursor.moveToFirst()) {
            result = true;
        }
        cursor.close();
        return result;
    }

    public void delete(int server_id, String type) {
        SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
        db.execSQL("delete from readHistory2 where server_id=? and type=?", new String[]{server_id + "", type});
    }
}
